package com.quectel.business.common;

import cn.hutool.core.bean.BeanUtil;
import com.quectel.core.module.village.dto.VillageBuildingDto;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.dto.VillageFloorDto;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.service.VillageBuildingService;
import com.quectel.core.module.village.service.VillageFloorService;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.core.module.village.service.VillageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 通用查询
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-05-16 15:43:00
 */
@Component
@Slf4j
public class CommonBusiness {

    @DubboReference
    private VillageService villageService;
    @DubboReference
    private VillageBuildingService villageBuildingService;
    @DubboReference
    private VillageFloorService villageFloorService;
    @DubboReference
    private VillageRoomService villageRoomService;

    /**
     * 根据对象获取拼接地址信息
     *
     * @param object 包含 villageId projectId buildingId floorId roomId
     * @return
     */
    public String getAddressByVillageInfo(Object object) {
        Map<String, Object> param = BeanUtil.beanToMap(object);
        StringBuilder sb = new StringBuilder();
        try {
            Long roomId = param.get("roomId") == null ? null : (Long) param.get("roomId");
            Long floorId = param.get("floorId") == null ? null : (Long) param.get("floorId");
            Long buildingId = param.get("buildingId") == null ? null : (Long) param.get("buildingId");
            Long villageId = param.get("villageId") == null ? null : (Long) param.get("villageId");
            if (roomId != null) {
                VillageRoomDto villageRoomDto = villageRoomService.selectCacheById(roomId);
                if (villageRoomDto != null) {
                    sb.append(villageRoomDto.getVillageName()).append("-")
                            .append(villageRoomDto.getBuildingName()).append("-")
                            .append(villageRoomDto.getFloorName()).append("-")
                            .append(villageRoomDto.getName());
                }
            } else if (floorId != null) {
                VillageFloorDto villageFloorDto = villageFloorService.selectCacheById(floorId);
                if (villageFloorDto != null) {
                    sb.append(villageFloorDto.getVillageName()).append("-")
                            .append(villageFloorDto.getBuildingName()).append("-")
                            .append(villageFloorDto.getName());
                }
            } else if (buildingId != null) {
                VillageBuildingDto villageBuildingDto = villageBuildingService.selectCacheById(buildingId);
                if (villageBuildingDto != null) {
                    sb.append(villageBuildingDto.getVillageName()).append("-")
                            .append(villageBuildingDto.getName());
                }
            } else if (villageId != null) {
                VillageDto villageDto = villageService.selectCacheById(villageId);
                if (villageDto != null) {
                    sb.append(villageDto.getName());
                }
            }

        } catch (Exception e) {
            log.error("拼装地址出错", e);
        }


        return sb.toString();
    }

}
